import http from './http'
import emojiList from './emoji';

function formateDate(value:string|number|Date){
    const dateNow = new Date()
    const date = new Date(value)

    // 计算时间差（毫秒）
    const diffMs = dateNow.getTime() - date.getTime();
    
    // 时间差转换为秒、分、小时、天
    const seconds = Math.floor(diffMs / 1000);
    const minutes = Math.floor(seconds / 60);
    const hours = Math.floor(minutes / 60);
    const days = Math.floor(hours / 24);
    
    if (seconds < 60) {
        return `刚刚`;
    } 
    else if (minutes < 60) {
        return `${minutes} 分钟前`;
    }
    else if (hours < 24) {
        return `${hours} 小时前`;
    }
    else if (days < 7) {
        return `${days} 天前`;
    }
    else {
        // 超过一周显示完整日期
        const year = date.getFullYear();
        const month = String(date.getMonth() + 1).padStart(2, '0');
        const day = String(date.getDate()).padStart(2, '0');
        const hour = String(date.getHours()).padStart(2, '0');
        const minute = String(date.getMinutes()).padStart(2, '0');
        const second = String(date.getSeconds()).padStart(2, '0');
        
        return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
    }
    
}

function formateRealDate(value:string|number|Date){
    const date = new Date(value)

    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hour = String(date.getHours()).padStart(2, '0');
    const minute = String(date.getMinutes()).padStart(2, '0');
    const second = String(date.getSeconds()).padStart(2, '0');

    return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
}

function formateTime(time: number|null|undefined) { 
    if(!time) return '--:--';
    const myTime = time / 1000;
    const minutes = Math.floor(myTime / 60).toString();
    const seconds = Math.floor(myTime % 60).toString();
    return `${minutes.padStart(2, '0')}:${seconds.padStart(2, '0')}`;
}

function formatFileSize(size:number){
    if(size < 1024){
        return `${size}B`
    }else if(size < 1024 * 1024){
        return `${(size / 1024).toFixed(2)}KB`
    }else{
        return `${(size / (1024 * 1024)).toFixed(2)}MB`
    }
}

export default { http }

export { formateDate, emojiList, formateRealDate, formatFileSize, formateTime }